jQuery(document).ready(function () {
    if (window.parent.location.href != location.href) {
        window.open(location.href, '_parent');
    }
    $("#loginform").on("submit", function (e) {
        e.preventDefault();
        var username = $("#username").val();
        var password = $("#password").val();
        if ($.trim(username) == "" || $.trim(password) == "") {
            layer.alert("请您填写用户名密码");
            return;
        }
        var lay = layer.load(0, {
            shade: [0.1]
        });
        var me = $("#rememberMe").prop("checked");
        setCookie(username, me);
        $.ajax({
            url: ctx + "/login", data: {
                username: $("#username").val(), password: $("#password").val(), rememberMe: me
            }, type: "post", dataType: "json", timeout: 60000, success: function (data, st) {
                layer.close(lay);
                if (data != null && data.code == "200") {
                    window.location.href = data.data;
                } else {
                    layer.alert(data.message);
                }
            }, error: function (st, dt) {
                layer.close(lay);
                alert("发送失败，请重试或检查网络！");
            }
        });
    });
    getCookie();
});

function getCookie() {
    var u = $.cookie('username');
    var rem = $.cookie("rememberme");
    if (rem === "true") {
        $("#username").val(u);
        $("#rememberMe").prop("checked", true);
    }
}

function setCookie(u, r) {
    if (!r) {
        $.removeCookie("username");
        $.removeCookie("rememberme");
    } else {
        $.cookie('username', u, {expires: 7, path: '/'});
        $.cookie("rememberme", "true", {expires: 7, path: "/"});
    }
}
